package com.wsoft.hr.pm.controller;

import cn.hutool.core.collection.CollUtil;
import com.github.pagehelper.PageInfo;
import com.wsoft.hr.pm.entity.HrPmCycleEntity;
import com.wsoft.hr.pm.query.cycle.*;
import com.wsoft.hr.pm.service.IHrPmCycleService;
import com.wsoft.response.ResponsePage;
import com.wsoft.response.ResponseResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * 活动周期 前端控制器
 * @author zhanggukai
 * @since 2025年02月25日 11:13:43
 */
@RestController
@Tag(name = "考核周期")
@RequestMapping("/hr/pm/cycle")
public class HrPmCycleController {

    @Resource
    private IHrPmCycleService cycleService;

    @Operation(summary = "新增周期", description = "新增周期")
    @PostMapping("/insert")
    public ResponseResult insert(@RequestBody @Valid CycleInsertQuery query) {
        boolean b = cycleService.insert(query);
        if (b) {
            return ResponseResult.success("[sys.msg.success]");
        } else {
            return ResponseResult.failed("[sys.msg.failed]");
        }
    }

    @Operation(summary = "编辑周期", description = "编辑周期")
    @PostMapping("/update")
    public ResponseResult update(@RequestBody @Valid CycleUpdateQuery query) {
        boolean b = cycleService.update(query);
        if (b) {
            return ResponseResult.success("[sys.msg.success]");
        } else {
            return ResponseResult.failed("[sys.msg.failed]");
        }
    }

    @Operation(summary = "启用周期", description = "启用周期")
    @PostMapping("/enable")
    public ResponseResult enable(@RequestBody @Valid CycleUpdateEnableQuery query) {
        boolean b = cycleService.enableCycle(query);
        if (b) {
            return ResponseResult.success("[sys.msg.success]");
        } else {
            return ResponseResult.failed("[sys.msg.failed]");
        }
    }


    @Operation(summary = "周期详情", description = "周期详情")
    @GetMapping("/get")
    public ResponseResult get(@RequestParam("id") Long id) {
        return ResponseResult.success(cycleService.get(id));
    }


    @Operation(summary = "删除周期", description = "删除周期")
    @GetMapping("/delete")
    public ResponseResult delete(@RequestParam("id") Long id) {
        boolean b = cycleService.delete(id);
        if (b) {
            return ResponseResult.success("[sys.msg.success]");
        } else {
            return ResponseResult.failed("[sys.msg.failed]");
        }
    }

    @Operation(summary = "周期分页", description = "周期分页")
    @PostMapping("/list")
    public ResponseResult list(@RequestBody CycleListQuery query) {
        PageInfo<HrPmCycleEntity> page = cycleService.pageList(query);
        return ResponseResult.success(ResponsePage.restPage(page));
    }

    @Operation(summary = "是否首次添加考核周期", description = "是否首次添加考核周期")
    @PostMapping("/firstAddCycle")
    public ResponseResult firstAddCycle(@RequestBody CycleInsertQuery query) {
        return ResponseResult.success(cycleService.firstAddCycle(query));
    }

    @Operation(summary = "检查时间是否重叠", description = "检查时间是否重叠")
    @PostMapping("/checkTimeOverlap")
    public ResponseResult checkTimeOverlap(@RequestBody CycleUpdateQuery query) {
        return ResponseResult.success(cycleService.checkTimeOverlap(query));
    }

    @Operation(summary = "批量获取周期数据",description = "批量获取周期数据")
    @PostMapping("/getByIds")
    public ResponseResult getByIds(@RequestBody @Valid HrCycleGetQuery query) {
        if(CollUtil.isEmpty(query.getIds())){
            return ResponseResult.success(new ArrayList<>());
        }
        List<HrPmCycleEntity> list = cycleService.getByIds(query.getIds());
        return ResponseResult.success(list);
    }
}
